home *** CD-ROM | disk | FTP | other *** search
/ SVM Mac 58 / CD-ROM N°58.iso / navigateurs / Netscape Folder / Components / nsSidebar.js < prev    next >
Encoding:
Text File  |  2000-04-19  |  8.5 KB  |  238 lines  |  [TEXT/MOSS]

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is mozilla.org code.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) 1999 Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s): Stephen Lamm <slamm@netscape.com>
  20.  */
  21.  
  22. /*
  23.  * No magic constructor behaviour, as is de rigeur for XPCOM.
  24.  * If you must perform some initialization, and it could possibly fail (even
  25.  * due to an out-of-memory condition), you should use an Init method, which
  26.  * can convey failure appropriately (thrown exception in JS,
  27.  * NS_FAILED(nsresult) return in C++).
  28.  *
  29.  * In JS, you can actually cheat, because a thrown exception will cause the
  30.  * CreateInstance call to fail in turn, but not all languages are so lucky.
  31.  * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
  32.  * for portability reasons -- and even when you're building completely
  33.  * platform-specific code, you can't throw across an XPCOM method boundary.)
  34.  */
  35.  
  36. const DEBUG = true; /* set to false to suppress debug messages */
  37. const PANELS_RDF_FILE  = 66626; /* the magic number to find panels.rdf */
  38.  
  39. const SIDEBAR_PROGID   = "component://mozilla/sidebar";
  40. const SIDEBAR_CID      = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}");
  41. const CONTAINER_PROGID = "component://netscape/rdf/container";
  42. const LOCATOR_PROGID   = "component://netscape/filelocator";
  43. const nsISupports      = Components.interfaces.nsISupports;
  44. const nsIFactory       = Components.interfaces.nsIFactory;
  45. const nsISidebar       = Components.interfaces.nsISidebar;
  46. const nsIRDFContainer  = Components.interfaces.nsIRDFContainer;
  47. const nsIFileLocator   = Components.interfaces.nsIFileLocator;
  48. const nsIRDFRemoteDataSource = Components.interfaces.nsIRDFRemoteDataSource;
  49.  
  50. function nsSidebar()
  51. {
  52.     const RDF_PROGID = "component://netscape/rdf/rdf-service";
  53.     const nsIRDFService = Components.interfaces.nsIRDFService;
  54.     
  55.     this.rdf = Components.classes[RDF_PROGID].getService(nsIRDFService);
  56.     this.datasource_uri = getSidebarDatasourceURI(PANELS_RDF_FILE);
  57.     debug('datasource_uri is ' + this.datasource_uri);
  58.     this.resource = 'urn:sidebar:current-panel-list';
  59.     this.datasource = this.rdf.GetDataSource(this.datasource_uri);
  60. }
  61.  
  62. nsSidebar.prototype.nc = "http://home.netscape.com/NC-rdf#";
  63.  
  64. nsSidebar.prototype.setWindow =
  65. function (aWindow)
  66. {    
  67.     this.window = aWindow;    
  68. }
  69.  
  70. nsSidebar.prototype.isPanel =
  71. function (aContentURL)
  72. {
  73.     var container = 
  74.         Components.classes[CONTAINER_PROGID].createInstance(nsIRDFContainer);
  75.  
  76.     container.Init(this.datasource, this.rdf.GetResource(this.resource));
  77.     
  78.     /* Create a resource for the new panel and add it to the list */
  79.     var panel_resource = 
  80.         this.rdf.GetResource("urn:sidebar:3rdparty-panel:" + aContentURL);
  81.  
  82.     return (container.IndexOf(panel_resource) != -1);
  83. }
  84.  
  85.  
  86. /* decorate prototype to provide ``class'' methods and property accessors */
  87. nsSidebar.prototype.addPanel =
  88. function (aTitle, aContentURL, aCustomizeURL)
  89. {
  90.     debug("addPanel(" + aTitle + ", " + aContentURL + ", " +
  91.           aCustomizeURL + ")");
  92.  
  93.     if (!this.window)
  94.     {
  95.         debug ("no window object set, bailing out.");
  96.         throw Components.results.NS_ERROR_NOT_INITIALIZED;
  97.     }
  98.  
  99.     /* Create a "container" wrapper around the
  100.      * "urn:sidebar:current-panel-list" object. This makes it easier
  101.      * to manipulate the RDF:Seq correctly. */
  102.     var container = 
  103.         Components.classes[CONTAINER_PROGID].createInstance(nsIRDFContainer);
  104.     container.Init(this.datasource, this.rdf.GetResource(this.resource));
  105.     
  106.     /* Create a resource for the new panel and add it to the list */
  107.     var panel_resource = 
  108.         this.rdf.GetResource("urn:sidebar:3rdparty-panel:" + aContentURL);
  109.     var panel_index = container.IndexOf(panel_resource);
  110.     if (panel_index != -1)
  111.     {
  112.         this.window.alert(aContentURL + " already exists in your sidebar.");
  113.         return;
  114.     }
  115.     
  116.     var rv = this.window.confirm("Add " + aContentURL + " to your sidebar?");
  117.     if (!rv)
  118.         return;
  119.  
  120.     this.datasource.Assert(this.rdf.GetResource(this.resource),
  121.                            this.rdf.GetResource(this.nc + "inbatch"),
  122.                            this.rdf.GetLiteral("true"),
  123.                            true);
  124.  
  125.     /* Now make some sidebar-ish assertions about it... */
  126.     this.datasource.Assert(panel_resource,
  127.                            this.rdf.GetResource(this.nc + "title"),
  128.                            this.rdf.GetLiteral(aTitle),
  129.                            true);
  130.     this.datasource.Assert(panel_resource,
  131.                            this.rdf.GetResource(this.nc + "content"),
  132.                            this.rdf.GetLiteral(aContentURL),
  133.                            true);
  134.     if (aCustomizeURL)
  135.         this.datasource.Assert(panel_resource,
  136.                                this.rdf.GetResource(this.nc + "customize"),
  137.                                this.rdf.GetLiteral(aCustomizeURL),
  138.                                true);
  139.         
  140.     container.AppendElement(panel_resource);
  141.  
  142.     this.datasource.Unassert(this.rdf.GetResource(this.resource),
  143.                              this.rdf.GetResource(this.nc + "inbatch"),
  144.                              this.rdf.GetLiteral("true"));
  145.  
  146.     /* Write the modified panels out. */
  147.     this.datasource.QueryInterface(nsIRDFRemoteDataSource).Flush();
  148.  
  149. }
  150.  
  151. var sidebarModule = new Object();
  152.  
  153. sidebarModule.registerSelf =
  154. function (compMgr, fileSpec, location, type)
  155. {
  156.     debug("registering (all right -- a JavaScript module!)");
  157.     compMgr.registerComponentWithType(SIDEBAR_CID, "Sidebar JS Component",
  158.                                       SIDEBAR_PROGID, fileSpec, location,
  159.                                       true, true, type);
  160. }
  161.  
  162. sidebarModule.getClassObject =
  163. function (compMgr, cid, iid) {
  164.     if (!cid.equals(SIDEBAR_CID))
  165.         throw Components.results.NS_ERROR_NO_INTERFACE;
  166.     
  167.     if (!iid.equals(Components.interfaces.nsIFactory))
  168.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  169.     
  170.     return sidebarFactory;
  171. }
  172.  
  173. sidebarModule.canUnload =
  174. function(compMgr)
  175. {
  176.     debug("Unloading component.");
  177.     return true;
  178. }
  179.     
  180. /* factory object */
  181. sidebarFactory = new Object();
  182.  
  183. sidebarFactory.CreateInstance =
  184. function (outer, iid) {
  185.     debug("CI: " + iid);
  186.     if (outer != null)
  187.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  188.  
  189.     if (!iid.equals(nsISidebar) && !iid.equals(nsISupports))
  190.         throw Components.results.NS_ERROR_INVALID_ARG;
  191.  
  192.     return new nsSidebar();
  193. }
  194.  
  195. /* entrypoint */
  196. function NSGetModule(compMgr, fileSpec) {
  197.     return sidebarModule;
  198. }
  199.  
  200. /* static functions */
  201. if (DEBUG)
  202.     debug = function (s) { dump("-*- sidebar: " + s + "\n"); }
  203. else
  204.     debug = function (s) {}
  205.  
  206. function getSidebarDatasourceURI(panels_file_id)
  207. {
  208.     try 
  209.     {
  210.         /* use the fileLocator to look in the profile directory 
  211.          * to find 'panels.rdf', which is the
  212.          * database of the user's currently selected panels. */
  213.         var fileLocatorService  =
  214.             Components.classes[LOCATOR_PROGID].getService(nsIFileLocator);
  215.  
  216.         /* if <profile>/panels.rdf doesn't exist, GetFileLocation() will copy
  217.          *bin/defaults/profile/panels.rdf to <profile>/panels.rdf */
  218.         var sidebar_file = fileLocatorService.GetFileLocation(panels_file_id);
  219.  
  220.         if (!sidebar_file.exists())
  221.         {
  222.             /* this should not happen, as GetFileLocation() should copy
  223.              * defaults/panels.rdf to the users profile directory */
  224.             debug("sidebar file does not exist");
  225.             return null;
  226.         }
  227.  
  228.         debug("sidebar uri is " + sidebar_file.URLString);
  229.         return sidebar_file.URLString;
  230.     }
  231.     catch (ex)
  232.     {
  233.         /* this should not happen */
  234.         debug("caught " + ex + " getting sidebar datasource uri");
  235.         return null;
  236.     }
  237. }
  238.